#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn=1000000, mods=998244353;

int n, m, ind[maxn+1], to[2*maxn+1], val[2*maxn+1], link[2*maxn+1];
ll f[maxn+1], fu[maxn+1], g[maxn+1], gu[maxn+1], h[maxn+1];

ll qpower(ll a, int n) {
    ll s=1;
    for (; n; n/=2) {
        if (n&1) s=s*a%mods;
        a=a*a%mods;
    }
    return s;
}

ll inc(ll &a, ll b) {
    return a=(a+b)%mods;
}

void addSide(int u, int v, int w) {
    m++;
    to[m] = v;
    val[m] = w;
    link[m] = ind[u];
    ind[u] = m;
}

void init(int o, int fa) {
    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            init(to[i], o);
            h[o] = max(h[o], h[to[i]]+val[i]);
        }
    }
}

void solve(int o, int fa, ll up) {
    int mh=0, sh=0;
    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            if (!mh || h[to[i]]+val[i]>h[to[mh]]+val[mh]) {
                sh=mh, mh=i;
            } else if (!sh || h[to[i]]+val[i]>h[to[sh]]+val[sh]) {
                sh=i;
            }
        }
    }
    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            int tar = i==mh ? sh : mh;
            solve(to[i], o, max(h[to[tar]]+val[tar], up)+val[i]);
        }
    }

    ll sumf=0;
    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            inc(f[o], fu[to[i]]*(sumf*2%mods+1)%mods);
            inc(fu[o], fu[to[i]]*2%mods);
            inc(sumf, fu[to[i]]);
        }
    }
    inc(f[o], 1);
    inc(fu[o], 1);

    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            ll temp=(val[i]*fu[to[i]]%mods+gu[to[i]])%mods;
            inc(g[o], temp*2%mods*(sumf-fu[to[i]])%mods+temp);
            int tar = i==mh ? sh : mh;
            inc(g[o], max(h[to[tar]]+val[tar], up)%mods*fu[to[i]]%mods);

            inc(gu[o], temp*2%mods);
        }
    }
    inc(g[o], max(h[to[mh]]+h[to[sh]]+val[mh]+val[sh], h[to[mh]]+up+val[mh])%mods);
    inc(gu[o], h[o]%mods);
}

int main() {
    freopen("path.in", "r", stdin);
    freopen("path.out", "w", stdout);

    int m;
    scanf("%d %d", &n, &m);
    for (int i=1; i<=m; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        addSide(u, v, w);
        addSide(v, u, w);
    }

    ll sumf=0, sumg=0;
    for (int i=1; i<=n; i++) {
        if (!f[i]) {
            init(i, 0);
            solve(i, 0, 0);
        }
        inc(sumf, f[i]), inc(sumg, g[i]);
    }

    printf("%lld", sumg*qpower(sumf, mods-2)%mods);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
